package com.example.cet4try.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.cet4try.MyApplication;
import com.example.cet4try.Utils.MySyntherizer;

@SuppressLint("Registered")
public class BaiduTTSActivity extends AppCompatActivity{

//    private static final String TAG = "BaiduTTS";

    protected MySyntherizer synthesizer;
    private SharedPreferences u_preferences;
    private SharedPreferences.Editor sp_editor;
    private boolean isStart = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        synthesizer = ((MyApplication)getApplicationContext()).getSpeechSynthesizer();

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        String username = preferences.getString("username", "");

        u_preferences = getSharedPreferences(username +"_Shared_Data", 0);
        sp_editor = u_preferences.edit();

    }

    protected void AddTimes(){
        if (isStart)
            return;
        isStart = true;
        int times = u_preferences.getInt("numoftimes",0);
        sp_editor.putInt("numoftimes",times+1);
        sp_editor.apply();
    }

    protected void AddRight(){
        int times = u_preferences.getInt("right_times",0);
        sp_editor.putInt("right_times",times+1);
        sp_editor.apply();
    }

    protected void speak(String text){
        //mShowText.setText("");
        //String text = mInput.getText().toString();
        // 需要合成的文本text的长度不能超过1024个GBK字节。
        /*if (TextUtils.isEmpty(mInput.getText())) {
            text = "百度语音，面向广大开发者永久免费开放语音合成技术。";
            mInput.setText(text);
        }*/
        // 合成前可以修改参数：
        // Map<String, String> params = getParams();
        // params.put(SpeechSynthesizer.PARAM_SPEAKER, "3"); // 设置为度逍遥
        // synthesizer.setParams(params);
        int result = synthesizer.speak(text);
        if (result != 0)
            Toast.makeText(BaiduTTSActivity.this,"播放失败",Toast.LENGTH_SHORT).show();
//        checkResult(result, "speak");
    }


    protected void reStart(){
        //number of time
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }

}
